package model;

import dao.FolderDao;
import dao.DaoFactory;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * This class is Folder model class
 * 
 * @author Sukhjinder K. Narula (narula.sk@gmail.com)
 * @since May 2013
 */
public class Folder {
	private final FolderDao folderDao;
	private int id;
	private String name;
	private int userId;
	private String error;
	private ArrayList<Email> mailList;

	/**
	 * Constructor
	 * 
	 * @param name
	 * @param userId
	 */
	public Folder(String name, int userId) {
		this.name = name;
		this.userId = userId;
		this.folderDao = DaoFactory.getFolderDao();
		this.mailList = new ArrayList<Email>();		
	}

	/**
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @return userId
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * 
	 * @param userId
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * 
	 * @return error
	 */
	public String getError() {
		return error;
	}

	/**
	 * 
	 * @param name
	 */
	public void setError(String error) {
		this.error = error;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean insert() {
		if (checkIfUserFolderExists(this.name, this.userId)) {
			return false;
		}
		try {
			this.id = folderDao.insert(createProperties());
		} catch (Exception e) {
			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean select(int id) {
		Map<String, String> folderProperties = null;
		try {
			folderProperties = folderDao.select(id);
		} catch (Exception e) {
			this.setError("Problem connecting to DB");
			return false;
		}
		this.id = id;
		this.name = folderProperties.get(name);
		this.userId = Integer.parseInt(folderProperties.get(userId));

		return true;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean update() {
		try {
			this.folderDao.update(createProperties());
		} catch (Exception e) {
			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * Delete the email from the db
	 */
	public boolean delete() {
		try {
			this.folderDao.delete(this.getId());
		} catch (Exception e) {
			this.setError("Problem connecting to DB");
			return false;
		}
		return true;
	}

	/**
	 * @return String
	 */
	public String toString() {
		return "";
	}

	/**
	 * 
	 * @return properties
	 */
	@SuppressWarnings("serial")
	private Map<String, String> createProperties() {
		return new HashMap<String, String>() {
			{
				put("id", String.valueOf(id));
				put("name", getName());
				put("userId", String.valueOf(getUserId()));
			}
		};
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean checkIfUserFolderExists(String name, int userId) {
		Map<String, String> folderProperties = null;
		try {
			folderProperties = folderDao.selectByNameAndUser(name, userId);
		} catch (Exception e) {
			this.setError("Problem connecting to db");
			return false;
		}
		if (!folderProperties.containsKey("error")) {
			return false;
		}
		this.setError(folderProperties.get("error"));
		return true;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean loadEmails() {
		Map<Integer, String[]> mail = null;
		try {
			// get the list of mails for this folder
			mail = DaoFactory.getEmailDao().selectMails(this.id);
			for (Map.Entry<Integer, String[]> entryMail : mail.entrySet()) {
				String mailArray[] = entryMail.getValue();
				Email emailObj = new Email(mailArray[0], mailArray[1], mailArray[2],
								mailArray[3], Boolean.valueOf(mailArray[4]), Timestamp
								.valueOf(mailArray[5]), Integer
								.valueOf(mailArray[6]));
				emailObj.setId(entryMail.getKey());
				mailList.add(emailObj);
			}
		} catch (Exception e) {
			this.setError("DB Connection problem");
			return false;
		}

		return true;
	}

}
